/*
 * Description: 
 *     History: yang@haipo.me, 2017/03/16, create
 */

# pragma once

# include "me_order.h"

extern uint64_t order_id_start;
extern uint64_t deals_id_start;

struct dict_id_key;

struct market_t : public nocopy_dict_value{
    char            *name;
    char            *stock;
    char            *money;

    int             stock_prec;
    int             money_prec;
    int             fee_prec;
    mpd_t           *min_amount;

    dict_plus<dict_id_key*, order_t*>     *orders;
    dict_plus<dict_id_key*, skiplist_t*>  *users;

    skiplist_t      *asks;
    skiplist_t      *bids;

    static market_t *create(market *conf);
    static sds status(sds reply);

    int get_status(size_t *ask_count, mpd_t *ask_amount, size_t *bid_count, mpd_t *bid_amount);

    int put_limit_order(bool real, json_t **result, uint32_t user_id, uint32_t side, mpd_t *amount, mpd_t *price, mpd_t *taker_fee, mpd_t *maker_fee, const char *source);

    int put_market_order(bool real, json_t **result, uint32_t user_id, uint32_t side, mpd_t *amount, mpd_t *taker_fee, const char *source);

    int cancel_order(bool real, json_t **result, order_t *order);

    int put_order(order_t *order);

    order_t *get_order(uint64_t id);

    skiplist_t *get_order_list(uint32_t user_id);

private:
    int execute_limit_ask_order(bool real, order_t *taker);

    int execute_limit_bid_order(bool real, order_t *taker);

    int execute_market_ask_order(bool real, order_t *taker);

    int execute_market_bid_order(bool real, order_t *taker);

    int order_put(order_t *order);

    int order_finish(bool real, order_t *order);
};

